package com.wk.cart.controller;

import com.wk.cart.entity.Cart;
import com.wk.cart.service.CartService;
import com.wk.common.bean.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 根据用户ID查询购物车中选中的商品
     */
    @GetMapping("checked/{userId}")
    public Resp<List<Cart>> queryCheckedCartByUserId(@PathVariable(value = "userId") Long userId){
        List<Cart> checkedCarts = cartService.queryCheckedCartByUserId(userId);
        return Resp.ok(checkedCarts);
    }

    //删除购物车中商品
    @DeleteMapping("{skuId}")
    public Resp<Void> deleteCarts(@PathVariable(value = "skuId") Long skuId){
        cartService.deleteCarts(skuId);
        return Resp.ok(null);
    }

    /**
     * 更新购物车商品数量
     */
    @PutMapping
    public Resp<Void> updateCarts(@RequestBody Cart cart){
        cartService.updateCarts(cart);
        return Resp.ok(null);
    }

    /**
     * 查询登录/未登录状态下的购物车
     */
    @GetMapping
    public Resp<List<Cart>> queryCarts(){
        List<Cart> carts = cartService.queryCarts();
        return Resp.ok(carts);
    }


    /**
     * 商品添加到购物车
     */
    @PostMapping
    public Resp<Void> addCart(@RequestBody Cart cart){
        cartService.addCart(cart);
        return Resp.ok(null);
    }

}
